﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.VisualBasic.Devices;
using System.Collections;

namespace IMGOrderName
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = string.Empty;
            Console.Write("目录地址：");
            while (string.IsNullOrWhiteSpace((path = Console.ReadLine())))
            {
                Console.Clear();
                Console.Write("目录地址：");
            }
            var files = Directory.GetFiles(path);
            Computer computer = new Computer();
            files.ToList().ForEach(a => computer.FileSystem.RenameFile(a, Guid.NewGuid() + Path.GetExtension(a)));

            files = Directory.GetFiles(Path.GetDirectoryName(files.First()));
            var groupFileWithSuffix = files.GroupBy(a => Path.GetExtension(a).ToUpper());

            groupFileWithSuffix.ToList().ForEach(a => RenameImage(a.ToList(), a.Key));
            
            Console.WriteLine("......");
            Console.ReadKey();
        }

        static void RenameImage(List<string> files, string suffix)
        {
            Computer computer = new Computer();
            IComparer<string> comparer = new OrderByFile();
            files.Sort(comparer);

            files.Aggregate(0, (t, n) =>
            {
                computer.FileSystem.RenameFile(n, "IMG_" + t.ToString().PadLeft(4, '0') + suffix);
                return ++t;
            });
        }
    }
    class OrderByFile : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            FileInfo file = new FileInfo(x.ToString());
            FileInfo file2 = new FileInfo(y.ToString());
            if (file.Length == file2.Length) return 0;
            return file.Length > file2.Length ? 1 : -1;
        }
    }
}